package com.fedoom.frontend.models;


import java.util.Iterator;
import java.util.Vector;

/**
 * (I don't know if we'll need this since ArgCollection looks like it does this. - MM).
 * 
 * This class is used to build and retrieve command line arguments from the GUI options
 * the user selected.  NOTE: this is not the list of all available command line arguments.
 */
public class CommandArgs {
	
	private Vector<String> CommandArgs;				// I may refer to this as a 'list' in the comments
	private String DoomLegacyExecutablePath;
	private static final String DEFAULT_LEGACY_PATH = "./llxdoom" + " ";
	
	/** Use if setting Doom Legacy Executable path later or not setting a path at all.
	 * 
	 */
	public CommandArgs() 
	{
		super();
		CommandArgs = new Vector<String>();
	}
	
	/** Use default legacy path (i.e. ./llxdoom). boolean isDefaultLegacyPath is used to allow
	 * 	a distinguishable constructor that allows the default Legacy path to be used.
	 * @param isDefaultLegacyPath
	 */
	public CommandArgs(boolean isDefaultLegacyPath)
	{
		super();
		CommandArgs = new Vector<String>();
		this.DoomLegacyExecutablePath = DEFAULT_LEGACY_PATH;
	}
	
	/** Use if defining a Legacy path.
	 * 
	 * @param doomLegacyExecutablePath
	 */
	public CommandArgs(String doomLegacyExecutablePath) 
	{
		super();
		CommandArgs = new Vector<String>();
		DoomLegacyExecutablePath = doomLegacyExecutablePath;
	}
	
	/**Add command line argument to the list 
	 * 
	 * @param CommandArg
	 */
	public void addCommandArg(String CommandArg) 
	{
		this.CommandArgs.add(CommandArg);
	}
	
	/** Remove command line argument from the list
	 * 
	 * @param CommandArg
	 */
	public void RemoveCommandArg(String CommandArg)
	{
		this.CommandArgs.remove(CommandArg);
	}
	
	/**
	 *  Retrieve a command line argument.  Return "" if not in list.
	 * @param CommandArg
	 * @return String CommandArg
	 */
	public String GetCommandArg(String CommandArg) 
	{
		if (this.CommandArgs.contains(CommandArg) == false) 
			CommandArg = "";
		
		return CommandArg;
	}
	
	/**
	 * Checks if argument is in list.  
	 * Returns true if in list. Returns false if not in list.
	 * @param CommandArg
	 * @return boolean IsCommandArgExist
	 */
	public boolean ContainsArg(String CommandArg)
	{
		boolean IsCommandArgExist = false;
		
		if (this.CommandArgs.contains(CommandArg))
			IsCommandArgExist = true;
		
		return IsCommandArgExist;
	}
	
	/**
	 *  Returns the command line argument list as a vector.
	 * @return
	 */
	public Vector<String> getCommandArgs() 
	{
		return CommandArgs;
	}
	
	/**
	 * Sets the command line arguments list as a vector.
	 * @param commandArgs
	 */
	public void setCommandArgs(Vector<String> commandArgs) 
	{
		CommandArgs = commandArgs;
	}
	
	/**
	 * Returns the Doom Legacy executable path.
	 * @return String DoomLegacyExecutablePath
	 */
	public String getDoomLegacyExecutablePath() 
	{
		return DoomLegacyExecutablePath;
	}
	
	/**
	 *  Sets the Doom Legacy executable path.
	 * @param doomLegacyExecutablePath
	 */
	public void setDoomLegacyExecutablePath(String doomLegacyExecutablePath) 
	{
		DoomLegacyExecutablePath = doomLegacyExecutablePath;
	}
	
	/**
	 *  Returns the default Doom Legacy executable path (i.e. ./llxdoom)
	 * @return String DEFAULT_LEGACY_PATH
	 */
	public static String getDefaultLegacyPath() 
	{
		return DEFAULT_LEGACY_PATH;
	}
	
	@Override
	/**
	 *  Returns the command line argument list as a string.
	 *  @return String CommandArgs
	 */
	public String toString() {
		String CommandArgs = "";
		
		CommandArgs = iterateAndReturnCommandArgs(CommandArgs);
		
		return CommandArgs;
	}

	private String iterateAndReturnCommandArgs(String CommandArgs) {
		Iterator itr = this.CommandArgs.iterator();
		
		while(itr.hasNext())
			CommandArgs += itr.next();
		
		return CommandArgs;
	}
	
	/**
	 *  Returns the Doom Legacy Executable path and command line argument list as a string.
	 * @param UseDefaultLegacyPath
	 * @return String LegacyPathAndCommandArgs
	 */
	public String toStringWithLegacyPath(boolean UseDefaultLegacyPath) 
	{
		String LegacyPathAndCommandArgs = "";
		
		LegacyPathAndCommandArgs = selectLegacyExecutablePath(UseDefaultLegacyPath);
		
		LegacyPathAndCommandArgs = iterateAndReturnCommandArgs(LegacyPathAndCommandArgs);
		
		return LegacyPathAndCommandArgs;
	}

	/**
	 * Gets either the default Doom Legacy executable path
	 * or the defined executable path if set by the constructor 
	 * or setDoomLegacyExecutablePath
	 * @param UseDefaultLegacyPath
	 * @return LegacyPath
	 */
	private String selectLegacyExecutablePath(boolean UseDefaultLegacyPath) {
		String LegacyPath;
		
		if (UseDefaultLegacyPath)
			LegacyPath = DEFAULT_LEGACY_PATH;
		else
			LegacyPath = this.DoomLegacyExecutablePath;
		
		return LegacyPath;
	}
}
